﻿// Printing.fs
//
// Copyright 2009 Sergey Mechtaev
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation.


#light

namespace SmartCPP.Core.Util

open SmartCPP.Core.Preprocessor
open SmartCPP.Core.Parser.Lexer

module Printing =
    
    ///Convert history to string
    let print_history text =
        
        let tab = 10
        
        ///Generate string with num spaces
        let rec spaces num = 
            if (num = 0) 
            then "" 
            else if (num = 1) 
                then " " 
                else " "+(spaces (num-1))
        
        ///Print history with offset from start of each lines 
        let rec format_print_history text offset =
            match text with
                | Binding.File (length, name, content) ->
                    (spaces offset)+"File:"+name+"\n"+
                    (spaces offset)+"File.length="+(length.ToString())+"\n"                
                | Binding.String (length, content) ->
                    (spaces offset)+"String:"+content+"\n"+
                    (spaces offset)+"String.length="+(length.ToString())+"\n"
                | Binding.Substring (text, segment) ->
                    (spaces offset)+"Substring:"+"\n"+
                    (format_print_history text (offset+tab))+
                    (spaces offset)+"Substring.segment="+(segment.ToString())+"\n"
                | Binding.Changed (length, text, seg, newtext)->
                    (spaces offset)+"Changed:"+"\n"+
                    (format_print_history text (offset+tab))+
                    (spaces offset)+"Changed.segment="+(seg.ToString())+"\n"+
                    (spaces offset)+"Changed.length="+(length.ToString())+"\n"+
                    (spaces offset)+"Changed.new="+"\n"+
                    (format_print_history newtext (offset+tab))               
        format_print_history text 0
        
    ///Pring preimage to console
    let print_preimage preimage converter = 
        let print_preimage_element element =             
            let source, seg, content = element            
            printf "%A" source
            printf " "
            printf "%A" (converter seg)
            printf " "
            printf "%A" content
            printfn ""           
        List.map print_preimage_element preimage |> ignore